package com.eightbears.beboy.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.ShowToast;
import com.hyphenate.easeui.EaseUI;
import com.umeng.analytics.MobclickAgent;

import java.io.Serializable;

/**
 * Created by baoga on 2017/7/2.
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        if (!isTaskRoot()) {
            Intent intent = getIntent();
            String action = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }
        mContext = this;
    }

    public Bundle getExtras() {
        return getIntent().getExtras();
    }

    public void startLocalActivity(Intent intent) {
        startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    public void startLocalActivity(Activity activity, Class cls) {
        Intent intent = new Intent(activity, cls);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    public void startLocalActivityAndClear(Activity activity, Class cls) {
        Intent logoutIntent = new Intent(activity, cls);
        logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(logoutIntent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    /**
     * 判断当前网络是否已连接
     */
    public boolean isNetWorkConnected(Context context) {
        boolean result;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        result = netInfo != null && netInfo.isConnected();
        if(!result) {
            ShowToast.showShortToast(getString(R.string.Network_error));
        }
        return result;
    }

    public void startLocalActivity(Activity activity, Class cls, String key, Object params,String key1, Object params1) {
        Intent intent = new Intent(activity, cls);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(key, (Serializable) params);
        mBundle.putSerializable(key1, (Serializable) params1);
        intent.putExtras(mBundle);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }
    public void startLocalActivity(Activity activity, Class cls, String key, Object params) {
        Intent intent = new Intent(activity, cls);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(key, (Serializable) params);
        intent.putExtras(mBundle);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }
    public void startLocalActivity(Activity activity, Class cls, String[] key, String[] params) {
        Intent intent = new Intent(activity, cls);
        for (int i = 0; i < key.length; i++) {
            intent.putExtra(key[i], params[i]);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    public void replaceFragment(int id_content, Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(id_content, fragment);
        transaction.commit();
    }

    public void replaceFragment(int id_content, Fragment fragment, String key, Object Value) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(id_content, fragment);
        Bundle bundle = new Bundle();
        bundle.putSerializable(key, (Serializable) Value);
        fragment.setArguments(bundle);
        transaction.commit();
    }
    public void replaceFragment(int id_content, Fragment fragment, String key, Object Value,String key1, Object Value1) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(id_content, fragment);
        Bundle bundle = new Bundle();
        bundle.putSerializable(key, (Serializable) Value);
        bundle.putSerializable(key1, (Serializable) Value1);
        fragment.setArguments(bundle);
        transaction.commit();
    }

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initEvent();

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    @Override
    protected void onResume() {
        super.onResume();
        EaseUI.getInstance().getNotifier().reset();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
